# -*-coding:Utf-8 -* 
""" UI Text box Class
"""
import bge, bgl, blf
from .box import *
from .color import *
from .image import *
from .font import *

CENTER = 1
LEFT = 0
RIGHT = 2

class TextBox(Box):
	
	DefaultSize = 14
	DefaultColor = Color((1.0,1.0,1.0))
	DefaultAlignement = 0
	DefaultPadding = (4,4,4,4)
	DefaultFont = Font("basic.ttf")
	
	def __init__(self, parent,x, y, text, width = None, height = None):
		self._text = text
		self._font = TextBox.DefaultFont
		self._size = TextBox.DefaultSize
		self.text_color = TextBox.DefaultColor
		self._alignement = TextBox.DefaultAlignement
		self._padding = TextBox.DefaultPadding
		blf.size(self.font, self.size,Font.dpi)
		self._dim = blf.dimensions(self.font, self.text)
		Box.__init__(self, parent,x, y, width, height)
		self.refresh_textposition()
		
	@property
	def text(self):
		return self._text
	@text.setter	
	def text(self, value):
		if type(value) != str:
			return
		self._text = value
		self.refresh_dimension()
		self.refresh_textposition()
		
	@property
	def font(self):
		return self._font.Id
	@font.setter	
	def font(self, value):
		if type(value) == str:
			value = Font(value)
		if type(value) != Font or not value.loaded:
			return
		self._font = value
		self.refresh_dimension()
		self.refresh_textposition()
		
	@property
	def size(self):
		return self._size
	@size.setter	
	def size(self, value):
		self._size = int(value)
		self.refresh_dimension()
		self.refresh_textposition()
		
	@property
	def alignement(self):
		return self._alignement
	@alignement.setter	
	def alignement(self, value):
		if not value in (0,1,2):
			return
		self._alignement = value
		self.refresh_textposition()
		
	@property
	def padding(self):
		return self._padding
		
	@padding.setter	
	def padding(self, value):
		if type(value) in (tuple,list):
			if len(value) == 2:
				self._padding = (round(value[0]),round(value[1]),round(value[0]),round(value[1]))
			elif len(value) == 4:
				self._padding = (round(value[0]),round(value[3]),round(value[2]),round(value[1]))
			else:
				value = round(value[0]) 
		if type(value) == int:
			self._padding = (value,value,value,value)
		self.refresh_textposition()
		
	def refresh_dimension(self):
		blf.size(self.font, self.size,Font.dpi)
		self._dim = blf.dimensions(self.font, self.text)
		if self.relative[2] == "text":
			self.w = "text"
		if self.relative[3] == "text":
			self.h = "text"
		
	
	def refresh_textposition(self):
		if self.w < self._dim[0] or self.h < self._dim[1]:
			self._textvisibility = False
		else:
			self._textvisibility = True
		padding = list(self.padding)
		if self.w < (self._dim[0]+padding[0]+padding[2]):
			padding[0] = round((self.padding[0]/(self._dim[0]+self.padding[0]+self.padding[2]))*self.w)
			padding[2] = round((self.padding[2]/(self._dim[0]+self.padding[0]+self.padding[2]))*self.w)
		if self.h < (self._dim[1]+padding[1]+padding[3]):
			padding[1] = round((self.padding[1]/(self._dim[1]+self.padding[1]+self.padding[3]))*self.h)
			padding[3] = round((self.padding[3]/(self._dim[1]+self.padding[1]+self.padding[3]))*self.h)
		if self.alignement == 0:
			self._textposition = (padding[0],(self.h/2)-(self._dim[1]/2))
		elif self.alignement == 2:
			self._textposition = (self.w-(padding[2]+self._dim[0]),(self.h/2)-(self._dim[1]/2))
		else:
			self._textposition = ((self.w/2)-(self._dim[0]/2),(self.h/2)-(self._dim[1]/2))
			
	def _setx(self, value):
		if type(value) == float and .0 < value <= 1.0:
			self.relative[0] = value
			value = round(self.parent.w*value)
		elif type(value) == float and -1.0 <= value < .0:
			self.relative[0] = value
			value = round(self.parent.w + (self.parent.w*value))
		elif type(value) in (int,float) and value < 0:
			self.relative[0] = value
			value = round(self.parent.w + value)
		elif type(value) == str and value != "" and value[0] == 'i':
			self.relative[0] = value
			value = value[1:]
			if 'i' in value:
				return
			if '.' in value:
				value=float(value)
			else:
				value = int(value)
			if type(value) == float and .0 < value <= 1.0:
				value = round((self.parent.w - (self.parent.w*value))-self.w)
			elif type(value) == float and -1.0 <= value < .0:
				value = round((self.parent.w*(-value))-self.w)
			elif value < 0:
				value = round(abs(value)-self.w)
			else:
				value = self.parent.w-(value+self.w)
		else:
			self.relative[0] = None
			value = round(value)
		self._x = int(value)
		self.check_visibility()
	
	def _sety(self, value):
		if type(value) == float and .0 < value <= 1.0:
			self.relative[1] = value
			value = round(self.parent.h*value)
		elif type(value) == float and -1.0 <= value < .0:
			self.relative[1] = value
			value = round(self.parent.h + (self.parent.h*value))
		elif type(value) in (int,float) and value < 0:
			self.relative[1] = value
			value = round(self.parent.h + value)
		elif type(value) == str and value != "" and value[0] == 'i':
			self.relative[1] = value
			value = value[1:]
			if 'i' in value:
				return
			if '.' in value:
				value=float(value)
			else:
				value = int(value)
			if type(value) == float and .0 < value <= 1.0:
				value = round((self.parent.h - (self.parent.h*value))-self.h)
			elif type(value) == float and -1.0 <= value < .0:
				value = round((self.parent.h*(-value))-self.h)
			elif value < 0:
				value = round(abs(value)-self.h)
			else:
				value = self.parent.h-(value+self.h)
		else:
			self.relative[1] = None
			value = round(value)
		self._y = int(value)
		self.check_visibility()
			
	def _setw(self, value):
		if value and type(value) != str and value < 0:
			value = -value
		if type(value) == float and .0 <= value <= 1.0:
			self.relative[2] = value
			value = round(self.parent.w*value)
		elif type(value) == str or value == None:
			value = round(self._dim[0]+self.padding[0]+self.padding[2])
			self.relative[2] = "text"
		else:
			self.relative[2] = None
			value = round(value)
		tmp = 0
		if not self._w == 0:
			tmp =  value/self._w
		self._w = int(value)
		if self.relative[0] != None and ((type(self.relative[0]) == str and self.relative[0] != "" and self.relative[0][0] == 'i') or (type(self.relative[0]) != str and self.relative[0] < 0)):
			self.x = self.relative[0]
		else:
			self.check_visibility()
		self.refresh_textposition()
		for aChild in self.childs:
			aChild.set_size_x(value,tmp)
	
	def _seth(self, value):
		if value and type(value) != str and value < 0:
			value = -value
		if type(value) == float and .0 < value <= 1.0:
			self.relative[3] = value
			value = round(self.parent.h*value)
		elif type(value) == str or value == None:
			value = round(self._dim[1]+self.padding[1]+self.padding[3])
			self.relative[3] = "text"
		else:
			self.relative[3] = None
			value = round(value)
		tmp = 0
		if not self._h == 0:
			tmp =  value/self._h
		self._h = int(value)
		if self.relative[1] != None and ((type(self.relative[1]) == str and self.relative[1] != "" and self.relative[1][0] == 'i') or (type(self.relative[1]) != str and self.relative[1] < 0)):
			self.y = self.relative[1]
		else:
			self.check_visibility()
		self.refresh_textposition()
		for aChild in self.childs:
			aChild.set_size_y(value,tmp)
		
	def _draw(self, x,y,alpha):
		Box._draw(self,x,y,alpha)
		if self.text and self._textvisibility:
			blf.disable(self.font, blf.SHADOW)
			blf.disable(self.font, blf.ROTATION)
			blf.size(self.font, self.size,Font.dpi)
			blf.position(self.font,x+self._textposition[0],y+self._textposition[1],0.0)
			if alpha:
				bgl.glColor4f(self.text_color.r,self.text_color.g,self.text_color.b,self.text_color.a*self.getAlpha())
			else:
				bgl.glColor4f(*self.text_color.all)
			blf.draw(self.font, self.text)
			bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
			bgl.glEnable(bgl.GL_TEXTURE_2D)
			bgl.glEnable(bgl.GL_BLEND)
			bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
